package com.tdzh.maimaiti.view;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.tdzh.maimaiti.R;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MyInterestActivity extends AppCompatActivity {

    private GridLayout needHelpCheckField;
    private Button needHelpConfirmButton;
    private GridLayout skillInCheckField;
    private Button skillConfirmButton;
    private ImageView back;
    private List<CheckBox> needHelpCheckList;
    private List<CheckBox> skillInCheckList;
    private Set<String> myNeed;
    private Set<String> mySkill;
    private SharedPreferences interest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        View view = getLayoutInflater().inflate(R.layout.activity_my_interest, null);
        interest = getSharedPreferences("interest", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = interest.edit();


        needHelpCheckList = new ArrayList<>();
        skillInCheckList = new ArrayList<>();
        myNeed = interest.getStringSet("myNeed", null);
        mySkill = interest.getStringSet("mySkill", null);

        back = (ImageView) view.findViewById(R.id.backArrow);
        needHelpCheckField = (GridLayout) view.findViewById(R.id.needHelpCheckField);
        skillInCheckField = (GridLayout) view.findViewById(R.id.skillInCheckField);
        needHelpConfirmButton = (Button) view.findViewById(R.id.needHelpConfirmButton);
        skillConfirmButton = (Button) view.findViewById(R.id.skillConfirmButton);

        String[] needData = null;
        String[] skillData = null;

        if(myNeed != null){
            needData = (String[]) myNeed.toArray(new String[myNeed.size()]);}
        else {
            myNeed = new HashSet<>();
            editor.putStringSet("myNeed", myNeed);

        }
        if(mySkill != null){
        skillData = (String[]) mySkill.toArray(new String[mySkill.size()]);}
        else {
            mySkill = new HashSet<>();
            editor.putStringSet("mySkill", mySkill);
        }

        setContentView(view);
        editor.commit();
        String[] items = getResources().getStringArray(R.array.sortName);
        for(int i = 0; i < items.length - 1; i++){
            CheckBox field = new CheckBox(this);
            field.setText(items[i + 1]);
            field.setTag(i);
            field.setTextSize(20);

            if(needData != null && needData.length > 0){

                for (int j = 0; j < needData.length; j++){
                    if(needData[j].equals(field.getTag())){
                        field.setChecked(true);

                    }
              }
            }

            GridLayout.Spec rowSpec = GridLayout.spec(i / 3);
            GridLayout.Spec columnSpec = GridLayout.spec(i % 3);
            GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams(rowSpec, columnSpec);

            needHelpCheckField.addView(field, layoutParams);
            needHelpCheckList.add(field);
            CheckBox field2 = new CheckBox(this);
            field2.setText(items[i + 1]);
            field2.setTag(i);
            field2.setTextSize(20);
            if(skillData != null && skillData.length > 0){
                for (int j = 0; j < skillData.length; j++){
                    if(skillData[j].equals(field2.getTag())){
                        field2.setChecked(true);
                    }
                }
            }


           // layoutParams.
            skillInCheckField.addView(field2 ,layoutParams);
            skillInCheckList.add(field2);

        }



        needHelpConfirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SharedPreferences.Editor delete = interest.edit();
                delete.remove("myNeed");
                delete.commit();

                if(myNeed == null){
                    myNeed = new HashSet<String>();
                }
                for (int i = 0; i < needHelpCheckList.size(); i++){
                    CheckBox field = (CheckBox) needHelpCheckField.findViewWithTag(i);
                    if(field.isChecked()){
                        myNeed.add(String.valueOf(i));
                        System.out.println(field.getText().toString());
                    }
                }

                SharedPreferences.Editor add = interest.edit();
                add.putStringSet("myNeed", myNeed);
                add.commit();

                System.out.println(myNeed.toString());
             }
        });

        skillConfirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SharedPreferences.Editor delete = interest.edit();
                delete.remove("mySkill");
                delete.commit();

                if(mySkill == null){
                    mySkill = new HashSet<String>();
                }
                for (int i = 0; i < skillInCheckList.size(); i++){
                    CheckBox field = (CheckBox) skillInCheckField.findViewWithTag(i);
                    if(field.isChecked()){
                        mySkill.add(String.valueOf(i));
                    }else {
                        mySkill.remove(field.getText().toString());
                    }
                }

                SharedPreferences.Editor add = interest.edit();
                add.putStringSet("mySkill", mySkill);
                add.commit();

                System.out.println(mySkill.toString());
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
